package com.student.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.student.dao.TuserDao;
import com.student.model.Colleges;
import com.student.model.Tuser;
import com.student.model.UserType;
import com.student.util.PageInfo;
import com.student.util.UserRoles;

@Component("tuserDao")
public class TuserDaoImpl extends HibernateDaoImpl implements TuserDao {

	public void delete(Long id) throws Exception {
		this.delete(Tuser.class, id);
	}

	@SuppressWarnings("unchecked")
	public List getAllTuser(PageInfo pageInfo) throws Exception {
		final String hql = "FROM Tuser t order by t.userId desc";
		List<Tuser> list = null;
		if (pageInfo == null) {
			list = this.find(hql);
		} else {
			list = this.findPageByQuery(hql, null, pageInfo);
		}
		return list;
	}

	public boolean insert(String userName, String userPass, Long collegeId,
			Long userTypeId) throws Exception {
		try {
			String hql = "select count(u) from Tuser u where u.userName=?";
			int count = Integer.parseInt(this.find(hql,
					new String[] { userName }).get(0)
					+ "");
			if (count > 0) {
				return false;
			}

			Tuser user = new Tuser();
			Colleges colleges = null;
			UserType ut = null;
			if (null != collegeId && !"".equals(collegeId)) {
				colleges = (Colleges) this.get(Colleges.class, collegeId);
			}
			if (null != userTypeId && !"".equals(userTypeId)) {
				ut = (UserType) this.get(UserType.class, userTypeId);
			}
			user.setUserName(userName);
			user.setUserPassword(userPass);
			user.setColleges(colleges);
			user.setUserType(ut);
			this.save(user);

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
		return true;
	}

	public boolean update(String userName, String userPass, Long collegeId,
			Long userTypeId, Long id) throws Exception {
		try {
			String hql="select count(u) from Tuser u where u.userName=? and u.userId !=?";
			int count=Integer.parseInt(this.find(hql, new Object[]{userName,id}).get(0)+"");
			if(count>0){
				return false;
			}
			Tuser tuser = getTuserById(id);
			tuser.setUserName(userName);
			tuser.setUserPassword(userPass);
			if(collegeId!=null&&!"".equals(collegeId)){
				tuser.setColleges((Colleges) this.get(Colleges.class, collegeId));
			}
			tuser.setUserType((UserType) this.get(UserType.class, userTypeId));
			this.update(tuser);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
		return true;
	}

	public List getAllCollege() throws Exception {
		String hql = "FROM Colleges";
		List<Colleges> list = null;
		try {
			list = this.find(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public List getAllUserType() throws Exception {
		String hql = "FROM UserType ut where ut.typeName!=?";
		List<UserType> list = null;
		try {
			list = this.find(hql,new String[]{UserRoles.SUPER_ADMIN});
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
		return list;
	}

	public Tuser getTuserById(Long id) throws Exception {
		Tuser tuser = null;
		try {
			tuser = (Tuser) this.get(Tuser.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
		return tuser;
	}

}
